package com.api.user.service.impl;

import com.api.core.config.AuthUser;
import com.api.user.dao.UserFriendApplyMapper;
import com.api.user.enums.IdentificationStatusEnums;
import com.api.user.model.UserFriend;
import com.api.user.model.UserFriendApply;
import com.api.user.service.UserFriendApplyService;
import com.api.core.service.AbstractService;
import com.api.user.service.UserFriendService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/03/31.
 */
@Service
@Transactional
public class UserFriendApplyServiceImpl extends AbstractService<UserFriendApply> implements UserFriendApplyService {
    @Resource
    private UserFriendApplyMapper userFriendApplyMapper;

    @Resource
    private UserFriendApplyService userFriendApplyService;

    @Resource
    private UserFriendService userFriendService;

    @Override
    public Result add(Long userId, AuthUser fansId) {


        List<UserFriend> friends = userFriendService.getUserFriend(fansId.getId());
        if(friends.size() >=20 && fansId.getLevel().equals(IdentificationStatusEnums.USER_LEVEL_BASE)){
            return ResultGenerator.genFailResult(400,"Friend's capacity maximum is 20");
        }

        //判断A和B是不是朋友关系
        UserFriend userFriend = userFriendService.getFriend(userId,fansId.getId());
        if(userFriend !=null){
            return ResultGenerator.genFailResult(400,"Already friends");
        }
        //判断A之前有没有申请过B为好友，并且还在等待B通过
        UserFriendApply u = this.findByUserIdAndFansId(userId,fansId.getId());
        if(u!=null && u.getState() == 1){
            return ResultGenerator.genFailResult(400,"You have registered");
        }
        //判断B有没有申请过A为好友，并且还在等待A通过
        UserFriendApply u1 = this.findByUserIdAndFansIdA(userId,fansId.getId());
        if(u1!=null && u1.getState()==1){
            return userFriendService.agree(fansId.getId(),userId,2);
        }

        u = new UserFriendApply();
        u.setCreateTime(new Date().getTime());
        u.setFansId(fansId.getId());
        u.setIdolId(userId);
        u.setState(1);
        userFriendApplyService.save(u);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public UserFriendApply findByUserIdAndFansId(Long userId, Long fansId) {
        return userFriendApplyMapper.findByUserIdAndFansId(userId,fansId);
    }

    @Override
    public UserFriendApply findByUserIdAndFansIdA(Long userId, Long fansId) {
        return userFriendApplyMapper.findByUserIdAndFansIdA(userId,fansId);
    }

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = userFriendApplyMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}